package com.test.mainui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.test.aninterface.IPrint;
import com.test.aninterface.getActivityName;
import com.test.aninterface.spiFactory;
import com.test.aninterface.startIntent;

import java.util.ArrayList;

@getActivityName(id = "main", value = MainActivity.class)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_a, btn_b;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_a = findViewById(R.id.btn_a);
        btn_b = findViewById(R.id.btn_b);
        tv = findViewById(R.id.tv);
        btn_b.setOnClickListener(this);
        btn_a.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_a) {
            goA();
        }

        if (id == R.id.btn_b) {
            goB();
        }
    }

    public void load() {
        spiFactory<IPrint> spiFactory = new spiFactory<>(IPrint.class);
        ArrayList<IPrint> nameList = spiFactory.getIPrints();
        for (IPrint i : nameList) {
            tv.append(i.getClass().getName() + "\n");
        }
    }


    public void showClassName() {
        spiFactory<IPrint> spiFactory = new spiFactory<>(IPrint.class);
        ArrayList<String> nameList = spiFactory.getAllClassName();
        for (String name : nameList) {
            tv.append(name + "\n");
        }
    }

    public void goA() {
        spiFactory<startIntent> spiFactory = new spiFactory<>(startIntent.class);
        ArrayList<startIntent> startIntents = spiFactory.getIPrints();
        for (startIntent s : startIntents) {
            s.go("a");
        }
    }

    public void goB() {
        spiFactory<startIntent> spiFactory = new spiFactory<>(startIntent.class);
        ArrayList<startIntent> startIntents = spiFactory.getIPrints();
        for (startIntent s : startIntents) {
            s.go("b");
        }
    }

    private void clear() {
        tv.setText("");
    }
}